package com.dlc.shop.platform.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.app.dto.OrderRefundInterventionDTO;
import com.dlc.shop.bean.dto.OrderRefundDto;
import com.dlc.shop.bean.model.OrderRefund;
import com.dlc.shop.common.constants.PlatformInterventionStatus;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.service.OrderRefundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author yami
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/orderRefund")
@Tag(name = "订单退款")
public class OrderRefundController {

    private final OrderRefundService orderRefundService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:orderRefund:page')")
    @Operation(summary = "分页查询" , description = "分页查询")
    @Parameters(value = {
            @Parameter(name = "startTime", description = "开始时间" ),
            @Parameter(name = "endTime", description = "结束时间" )
    })
    public ServerResponseEntity<IPage<OrderRefundDto>> getOrderRefundPage(
            PageParam<OrderRefundDto> page, OrderRefundDto orderRefundDto,
            @RequestParam(required = false) String startTime,
                                                                    @RequestParam(required = false) String endTime) {
        IPage<OrderRefundDto> refundPage = orderRefundService.getPage(page, orderRefundDto, startTime, endTime, 1);
        List<OrderRefundDto> records = refundPage.getRecords();
        Date platformHandleTime = DateUtil.offsetDay(new Date(), -3).toJdkDate();
        for (OrderRefundDto record : records) {
            int handleIntervention = 2;
            if(Objects.nonNull(record.getPlatformInterventionStatus()) && Objects.equals(record.getPlatformInterventionStatus(), PlatformInterventionStatus.APPLY.value())) {
                record.setPlatformInterventionStatus(DateUtil.compare(platformHandleTime, record.getApplyInterventionTime()) > 0 ? PlatformInterventionStatus.HANDLE.value() : record.getPlatformInterventionStatus());
            }
        }
        return ServerResponseEntity.success(refundPage);
    }

    @GetMapping("/info")
    @PreAuthorize("@pms.hasPermission('platform:orderRefund:info')")
    @Operation(summary = "获取订单退款信息" , description = "获取订单退款信息")
    @Parameters(value = {
            @Parameter(name = "refundId", description = "退款id" ),
            @Parameter(name = "shopId", description = "店铺id" )
    })
    public ServerResponseEntity<OrderRefund> info(@RequestParam("refundId") Long refundId, @RequestParam("shopId") Long shopId) {
        OrderRefund orderRefund = orderRefundService.getOrderRefundById(refundId, shopId);
        return ServerResponseEntity.success(orderRefund);
    }

    /**
     * 平台处理退款介入
     */
    @PutMapping("/handlePlatformIntervention")
    @Operation(summary = "平台处理退款介入")
    public ServerResponseEntity<?> hanlePlatormIntervertion(@RequestBody OrderRefundInterventionDTO orderRefundInterventionDTO) {
        orderRefundService.hanlePlatormIntervertion(orderRefundInterventionDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/isLastRefund")
    @Operation(summary = "是否为最后一单退款")
    public ServerResponseEntity<Boolean> isLastRefund(String refundSn) {
        return ServerResponseEntity.success(orderRefundService.getIsLastRefund(refundSn, null));
    }

}
